package com.banciyuan.bcywebview.biz.detail.charge;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.imageview.CircleImageView;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/5/16.
 */
public class ChargeRankAdapter extends BaseAdapter {

    private List<UserDetail> ulist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;

    public ChargeRankAdapter(List<UserDetail> ulist, Context mContext) {
        this.ulist = ulist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return ulist.size() + 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    public Object getItem(int position) {
        return ulist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        public TextView rank_tv;
        public TextView uname;
        public ImageView avatar;
        public ImageView rank_iv;

        public ViewHolder(View convertView) {
            rank_tv = (TextView) convertView.findViewById(R.id.rank_tv);
            rank_iv = (ImageView) convertView.findViewById(R.id.rank_iv);
            uname = (TextView) convertView.findViewById(R.id.user_name);
            avatar = (CircleImageView) convertView.findViewById(R.id.user_head);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            if (getItemViewType(position) == 1) {
                convertView = inflater.inflate(R.layout.charge_rank_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                convertView = inflater.inflate(R.layout.charge_rank_item_head, null);
            }
        } else {
            if (getItemViewType(position) == 1) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = inflater.inflate(R.layout.charge_rank_item_head, null);
            }
        }
        if (getItemViewType(position) == 1) {
            final UserDetail userDetail = ulist.get(position - 1);
            if (!TextUtils.isEmpty(userDetail.getAvatar())) {
                ImageLoader.getInstance().displayImage(userDetail.getAvatar(), viewHolder.avatar, ImageOptions.optionHead);
            }
            if (!TextUtils.isEmpty(userDetail.getUname())) {
                viewHolder.uname.setText(userDetail.getUname());
            }
            viewHolder.rank_iv.setVisibility(View.VISIBLE);
            viewHolder.rank_tv.setVisibility(View.GONE);
            switch (position) {
                case 1:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_one, mContext.getTheme()));
                    } else {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_one));
                    }
                    break;
                case 2:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_two, mContext.getTheme()));
                    } else {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_two));
                    }
                    break;
                case 3:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_three, mContext.getTheme()));
                    } else {
                        viewHolder.rank_iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.charge_rank_three));
                    }
                    break;
                default:
                    viewHolder.rank_iv.setVisibility(View.GONE);
                    viewHolder.rank_tv.setVisibility(View.VISIBLE);
                    viewHolder.rank_tv.setText(String.valueOf(position));
                    break;
            }

            viewHolder.avatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, userDetail.getUid());
                }
            });
        }
        return convertView;
    }
}
